import java.awt.Color;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Playground extends JFrame {

	/**
	 * 
	 */
	private PlaygroundElement[][] playground;	
	private static final long serialVersionUID = 1L;
	public int width, height;
	public JPanel pn;
	/**
	 * @param args
	 * @throws IOException 
	 * @throws NumberFormatException 
	 */
	public Playground(int width,int height,String map,Game parent) throws NumberFormatException, IOException {
		super("Bomberman");
		this.width=width;
		this.height=height;
		setSize(width*50,height*50);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        pn=new JPanel();
        GridLayout layout = new GridLayout(height,width);
        pn.setLayout(layout);
        playground = new PlaygroundElement[width][height];
		InputStream in = this.getClass().getResourceAsStream(map);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		 String strLine;
		 int i = 0;
		 int playernum = 1;
		 while ((strLine = br.readLine()) != null)   {
			  for (int o=0; o<strLine.length(); o++){
				  switch (Integer.parseInt(strLine.substring(o, o+1))) {
				  case 0: 	playground[o][i]=null;
				  			break;
				  case 1:	playground[o][i]=new Wall(o,i,parent);
					  		break;
				  case 2:	playground[o][i]=new Player(o,i,parent,playernum); playernum++;
				  			break;
				  case 3:	playground[o][i]=new Dirt(o,i,parent);
		  					break;
				  }
				  //playground[o][i]=Integer.parseInt(strLine.substring(o, o+1));
				  //System.out.println(map[i][o]);
			  }
			  i++;
			  }
        in.close();
        add(pn);
        
       
        
	}
	
	public void drawPlayground() {
		//Random ran = new Random();
		pn.removeAll();
		 for (int i=0; i<height; i++) {
	        	for (int o=0; o<width; o++) {
	        		//System.out.println(playground[o][i]);
	        		
	        		//if (playground[o][i].getType()==) {
	        		//	JPanel p = new JPanel(); 
	        		//	p.setBackground(new Color(0,0,0));
	        		//	pn.add(p);
	        		//}
	        		if (playground[o][i]!=null) {
	        		if (playground[o][i].getType()==1) {
	        			JPanel p = new JPanel(); 
	        			p.setBackground(new Color(50,50,50));
	        			pn.add(p);
	        		}
	        		
	        		if (playground[o][i].getType()==3) {
	        			JPanel p = new JPanel(); 
	        			p.setBackground(new Color(100,100,100));
	        			pn.add(p);
	        		}	
	        		if (playground[o][i].getType()==4) {
	        			JPanel p = new JPanel(); 
	        			p.setBackground(new Color(0,0,200));
	        			pn.add(p);
	        		}	
	        		if (playground[o][i].getType()==2) {
	        			JPanel p = new JPanel(); 
	        			p.setBackground(new Color(200,0,0));
	        			pn.add(p);
	        		}
	        		}
	        		if (playground[o][i]==null) {
	        			//playground[o][i]=ran.nextInt(4);
	        			JPanel p = new JPanel();
	        			p.setBackground(new Color(0,200,0));
	        			pn.add(p);
	        		}
	        		
	        	}
	        }
		 pn.validate();
		 pn.repaint();
	}
	
	public void addObject(int x, int y, PlaygroundElement object) {
		playground[x][y]=object;
		drawPlayground();
	}
	
	public boolean moveObject(int xold, int yold, int xnew, int ynew) {
		if (playground[xnew][ynew]!=null) {return false;} 
		else {
		playground[xnew][ynew]=playground[xold][yold];
		playground[xold][yold]=null;
		drawPlayground();
		return true;
		}
	}
	
	public boolean removeObject(int x, int y) {
		if (playground[x][y]!=null){
		if ((playground[x][y].getType()==3)||(playground[x][y].getType()==4)) {
			playground[x][y]=null;
			drawPlayground();
			return true;
		} else {
			if(playground[x][y].getType()==2) {playground[x][y].die(); playground[x][y]=null; return true;} else 
			if (playground[x][y].getType()==1) return true; else return false;
		}	
		} else return false;
		
	}

}
